#!/usr/bin/make -f
#
# Copyright 2011 Google Inc. All Rights Reserved.
# Author: gavaletz@google.com (Eric Gavaletz)


# BUILD REQUIREMENTS

# java -- sudo apt-get install openjdk-7-jre
# TODO(user) add appcfg.py, dev_appserver.py and gjslint to your PATH.
# GAE -- http://code.google.com/appengine/
# gjslint -- http://code.google.com/p/closure-linter/

# http://code.google.com/p/closure-library/
closure_root = $(HOME)/SRC/closure-library/
builder = $(closure_root)closure/bin/build/closurebuilder.py

# http://code.google.com/p/closure-compiler/
compiler_jar = $(HOME)/SRC/closure-compiler/compiler.jar

# http://code.google.com/p/closure-stylesheets/
#TODO(user) you will need to create a symbolic link for closure-stylesheets.jar.
compiler_css = java -jar $(HOME)/SRC/closure-stylesheets/closure-stylesheets.jar


project_root= ./
GAE_STATIC = gae/net-score/static/
version = $(shell egrep "^version: " gae/net-score/app.yaml | cut -d " " -f 2)


cfb = --compiler_flags=
compiler_flags = $(cfb)"--compilation_level=ADVANCED_OPTIMIZATIONS"
compiler_flags += $(cfb)"--externs=externs/google_analytics_api.js"
compiler_flags += $(cfb)"--externs=externs/google_loader_api.js"
compiler_flags += $(cfb)"--externs=externs/google_maps_api_v3_6.js"
compiler_flags += $(cfb)"--externs=externs/google_visualization_api.js"
compiler_flags += $(cfb)"--externs=externs/youtube_js_api.js"
compiler_flags += $(cfb)"--externs=externs/ns_i18n_api.js"


compile = $(builder) \
	--root=$(closure_root) \
	--root=$(project_root) \
	--output_mode=compiled \
	--compiler_jar=$(compiler_jar) \
	$(compiler_flags) \
	--namespace=NAMESPACE \
	--output_file=


depends = $(builder) \
	--root=$(closure_root) \
	--root=$(project_root) \
	--output_mode=list \
	--compiler_jar=$(compiler_jar) \
	--namespace=NAMESPACE \


.PHONY: all
all: strings flock feather egg resultdetail resultlist developer css xml images


.PHONY: strings
strings: $(GAE_STATIC)$(version).strings-en-comp.js


.PHONY: flock
flock: $(GAE_STATIC)$(version).flock-comp.js


.PHONY: feather
feather: $(GAE_STATIC)$(version).feather-comp.js


.PHONY: egg
egg: $(GAE_STATIC)$(version).egg-comp.js


.PHONY: resultdetail
resultdetail: $(GAE_STATIC)$(version).result-detail-comp.js


.PHONY: resultlist
resultlist: $(GAE_STATIC)$(version).result-list-comp.js


.PHONY: developer
developer: $(GAE_STATIC)$(version).developer-comp.js


.PHONY: css
css: $(GAE_STATIC)$(version).flock-comp.css $(GAE_STATIC)$(version).egg-comp.css


.PHONY: xml
xml: $(GAE_STATIC)feather.xml $(GAE_STATIC)egg.xml $(GAE_STATIC)robots.txt


$(GAE_STATIC)$(version).strings-en-comp.js: $(shell $(depends:NAMESPACE=ns.i18n.en))
	$(compile:NAMESPACE=ns.i18n.en)$(GAE_STATIC)$(version).strings-en-comp.js


$(GAE_STATIC)$(version).flock-comp.js: $(shell $(depends:NAMESPACE=netscore))
	$(compile:NAMESPACE=netscore)$(GAE_STATIC)$(version).flock-comp.js


$(GAE_STATIC)$(version).feather-comp.js: $(shell $(depends:NAMESPACE=feather))
	$(compile:NAMESPACE=feather)$(GAE_STATIC)$(version).feather-comp.js


$(GAE_STATIC)$(version).egg-comp.js: $(shell $(depends:NAMESPACE=egg))
	$(compile:NAMESPACE=egg)$(GAE_STATIC)$(version).egg-comp.js


$(GAE_STATIC)$(version).result-detail-comp.js: $(shell $(depends:NAMESPACE=resultdetail))
	$(compile:NAMESPACE=resultdetail)$(GAE_STATIC)$(version).result-detail-comp.js


$(GAE_STATIC)$(version).result-list-comp.js: $(shell $(depends:NAMESPACE=resultlist))
	$(compile:NAMESPACE=resultlist)$(GAE_STATIC)$(version).result-list-comp.js


$(GAE_STATIC)$(version).developer-comp.js: $(shell $(depends:NAMESPACE=developer))
	$(compile:NAMESPACE=developer)$(GAE_STATIC)$(version).developer-comp.js


$(GAE_STATIC)$(version).flock-comp.css: flock-css/*.css
	$(compiler_css) --output-file $(GAE_STATIC)$(version).flock-comp.css flock-css/*.css


$(GAE_STATIC)$(version).egg-comp.css: egg-css/*.css
	$(compiler_css) --output-file $(GAE_STATIC)$(version).egg-comp.css egg-css/*.css


$(GAE_STATIC)robots.txt: robots.txt
	cp robots.txt $(GAE_STATIC)


$(GAE_STATIC)feather.xml: feather.xml
	cp feather.xml $(GAE_STATIC)


$(GAE_STATIC)egg.xml: egg.xml
	cp egg.xml $(GAE_STATIC)


.PHONY: images
images:
	rsync -av images $(GAE_STATIC)
